package com.gx.smart.lib.http.api;

import com.casic.gx.grpc.common.ComQuery;
import com.gx.smart.lib.http.api.asynctask.CallBack;
import com.gx.smart.lib.http.api.asynctask.GrpcAsyncTask;
import com.gx.smart.lib.http.api.config.ApiConfig;
import com.gx.smart.lib_http.BuildConfig;
import com.gx.wisestone.constructionappgrpclib.grpc.liftinfo.ConstructionAppLiftInfoDto;
import com.gx.wisestone.constructionappgrpclib.grpc.liftinfo.ConstructionAppLiftInfoProviderGrpc;
import com.gx.wisestone.constructionappgrpclib.grpc.liftinfo.ConstructionAppLiftInfoQueryDto;
import com.gx.wisestone.constructionappgrpclib.grpc.liftinfo.ConstructionAppLiftInfoQueryResponse;
import com.gx.wisestone.constructionappgrpclib.grpc.liftinfo.GenerateOrCodeDto;
import com.gx.wisestone.constructionappgrpclib.grpc.liftinfo.GenerateOrCodeResponse;
import com.orhanobut.logger.Logger;

import java.util.concurrent.TimeUnit;

import io.grpc.ManagedChannel;

/**
 * @author xiaosy
 * @create 2020/8/10
 * @Describe
 **/
public class ConstructionAppLiftInfoProviderService {

    private String host = BuildConfig.JOYLIFE_GRPC_SERIVER_URL;
    private String port = "46604";

    private ConstructionAppLiftInfoProviderService() {

    }

    public static ConstructionAppLiftInfoProviderService getInstance() {
        return SingleTonHolder.INSTANCE;
    }

    /**
     * 获取AuthStub  设置超时时间  8秒
     *
     * @param channel
     * @return
     */
    private ConstructionAppLiftInfoProviderGrpc.ConstructionAppLiftInfoProviderBlockingStub getStub(ManagedChannel channel) {
        return ConstructionAppLiftInfoProviderGrpc.newBlockingStub(channel)
                .withDeadlineAfter(ApiConfig.TIMEOUT_NETWORK, TimeUnit.SECONDS);

    }

    /**
     * 查询梯控信息
     * <p>
     * ws_grpc_com.ComQuery comQuery = 2; //公共查询体
     * string unit_id = 3;  //单元id
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, ConstructionAppLiftInfoQueryResponse> queryLiftInfo(
            final ComQuery comQuery,
            final String unitId,
            CallBack callBack) {
        return new GrpcAsyncTask<String, Void, ConstructionAppLiftInfoQueryResponse>(callBack) {
            @Override
            protected ConstructionAppLiftInfoQueryResponse doRequestData(ManagedChannel channel) {
                ConstructionAppLiftInfoQueryDto message = ConstructionAppLiftInfoQueryDto.newBuilder()
                        .setComQuery(comQuery)
                        .setDto(ConstructionAppLiftInfoDto
                                .newBuilder()
                                .setUnitId(unitId)
                                .build())
                        .build();
                Logger.d(message);
                ConstructionAppLiftInfoQueryResponse response = null;
                try {
                    response = getStub(channel).queryLiftInfo(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "queryLiftInfo");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

    /**
     * 生成设置梯控号二维码字符串
     * string lift_id = 1;//梯控编号
     * int32 tenant_no = 2;//所选小区分区标识
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, GenerateOrCodeResponse> generateOrCode(
            final String lifeId,
            final int tenantNo,
            CallBack callBack) {
        return new GrpcAsyncTask<String, Void, GenerateOrCodeResponse>(callBack) {
            @Override
            protected GenerateOrCodeResponse doRequestData(ManagedChannel channel) {
                GenerateOrCodeDto message = GenerateOrCodeDto.newBuilder()
                        .setLiftId(lifeId)
                        .setTenantNo(tenantNo)
                        .build();
                Logger.d(message);
                GenerateOrCodeResponse response = null;
                try {
                    response = getStub(channel).generateOrCode(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "generateOrCode");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

    private static class SingleTonHolder {
        private static final ConstructionAppLiftInfoProviderService INSTANCE = new ConstructionAppLiftInfoProviderService();
    }

}
